<template>

  <el-table :data="tableData" border style="width: 100%" height="250">
    <el-table-column fixed prop="id" label="工号" width="250"/>
    <el-table-column prop="name" label="姓名" width="220"/>
    <el-table-column prop="major" label="系别" width="220"/>
    <el-table-column prop="role" label="身份角色" width="220"/>
    <el-table-column prop="operations" label="操作" width="250">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">Delete</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog v-model="dialogFormVisible" title="身份修改" width="30%" center>
    <el-form :model="form" label-position="top" style="display: inline-block">
      <el-input v-model="form.id" disabled type="hidden"/>
      <el-form-item label="老师名字">
        <el-input v-model="form.name" disabled/>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="form.role" placeholder="请选择您的身份">
          <el-option label="指导老师" value="1"/>
          <el-option label="专业负责人" value="2"/>
          <el-option label="系主任" value="3"/>
          <el-option label="教务处" value="4"/>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </span>
    </template>
  </el-dialog>

  <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[1, 3, 5, 10]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="justify-content: center;"
  />
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      tableData: [],
      form: ({
        id: '',
        name: '',
        role: '',
      }),
      dialogFormVisible: false,
      currentPage: 1,
      pageSize: 5,
      total: 10,
      small: false,
      background: true,
      disabled: false
    };
  },
  methods: {
    handleSizeChange(val) {
      axios.post('/teachers/all', {
        page: this.currentPage,
        pageSize: val,
      }).then(res => {
        let r = res.data
        if (r.flag === true) {
          this.total = r.total
          for (var i in r.datas) {
            if (r.datas[i].role === 1)
              r.datas[i].role = "指导老师"
            else if (r.datas[i].role === 2)
              r.datas[i].role = "专业负责人"
            else if (r.datas[i].role === 3)
              r.datas[i].role = "系主任"
            else
              r.datas[i].role = "教务处"
          }
          this.tableData = r.datas
        } else {
          alert(r.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    handleCurrentChange(val) {
      axios.post('/teachers/all', {
        page: val,
        pageSize: this.pageSize
      }).then(res => {
        let r = res.data
        if (r.flag === true) {
          this.total = r.total
          for (var i in r.datas) {
            if (r.datas[i].role === 1)
              r.datas[i].role = "指导老师"
            else if (r.datas[i].role === 2)
              r.datas[i].role = "专业负责人"
            else if (r.datas[i].role === 3)
              r.datas[i].role = "系主任"
            else
              r.datas[i].role = "教务处"
          }
          this.tableData = r.datas
        } else {
          alert(r.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    handleEdit(index, row) {
      this.form = row
      this.dialogFormVisible = true
    },
    handleDelete(index, row) {
      var result = confirm("你确定要删除吗？")
      if (result) {
        axios.post('/teachers/del/', {
          id: row.id,
        }).then(res => {
          let r = res.data
          alert(r.msg)
          this.dialogFormVisible = false
          this.$router.go(0)
        }).catch(err => {
          console.log(err)
        })
      }
    },
    submit() {
      axios.post('/teachers/update/' + this.form.role, {
        id: this.form.id,
      }).then(res => {
        let r = res.data
        alert(r.msg)
        this.dialogFormVisible = false
        this.$router.go(0)
      }).catch(err => {
        console.log(err)
      })
    },
  },
  mounted() {
    this.handleCurrentChange(1)
  },
};
</script>